package com.fluxtool.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fluxtool.entity.Quartzs;
import com.fluxtool.service.QuartzsService;
import com.fluxtool.utill.date.DateUtill;
import com.fluxtool.utill.scheduler.SpringBootCoreJob;


@Controller
@RequestMapping("/quartz")
public class QuartzsController {
	
	
	private static final Logger log = LoggerFactory.getLogger(QuartzsController.class);

	@Autowired
	private QuartzsService quartzsService;
	
	@RequestMapping("/quartzs")
	public String quartzsIndex(Model model, Page<Quartzs> page){
		IPage<Quartzs> pages  = quartzsService.page(page, null);
		model.addAttribute("pages", pages);
		return "quartzs";
	}
	
	@RequestMapping("/startQuartz")
	public String startQuartz(Long id){
		Quartzs quartz =  quartzsService.getById(id);
		quartz.setEnble(1);
		quartzsService.saveOrUpdate(quartz);
		return "quartzs";
	}
	
	@RequestMapping("/stopQuartz")
	public String stopQuartz(Long id){
		Quartzs quartz =  quartzsService.getById(id);
		quartz.setEnble(0);
		quartzsService.saveOrUpdate(quartz);
		return "quartzs";
	}
	
	@RequestMapping("/add")
	public String add(Long id, Model model){
		if(id!=0){
			Quartzs quartz = quartzsService.getById(id);
			model.addAttribute("quartz", quartz);
		}
		if(id==0){
			Quartzs quartz = new Quartzs();
			quartz.setEnble(0);
			model.addAttribute("quartz", quartz);
		}
		return "quartzadd";
	}
	
	@RequestMapping("/delete")
	public String deleteIp(Long id){
		quartzsService.removeById(id);
		return "quartzs";
	}
	
	@RequestMapping("/insertOrUpdate")
	public String save(Quartzs quartzs){
		if(quartzs.getId()==null||quartzs.getId()==0){
			quartzs.setCreateTime(DateUtill.getNowDate());
			quartzs.setEnble(0);
		}
		quartzsService.saveOrUpdate(quartzs);
		return "quartzs";
	}
	
	@RequestMapping("/startAll")
	@ResponseBody
	public String startAll(){
		new SpringBootCoreJob().quartzMain();
		return "true";
	}
	
	
}
